WEBVTT

00:00.940 --> 00:05.260
Hello and welcome back to this session about user defined functions.

00:05.260 --> 00:11.710
So whenever we want to reuse or repeat the same or very similar code more than once it may be worth

00:11.740 --> 00:14.170
writing a reusable function.

00:14.170 --> 00:19.480
And by doing so this can help us to make our code more readable.

00:19.480 --> 00:21.040
Functions are nothing new.

00:21.040 --> 00:25.180
And we use a lot of built in functions so far but.

00:25.270 --> 00:35.290
First of all let's create a list ATL containing four integers 3 2 5 6 and we already know the function

00:35.320 --> 00:43.350
the built in function print so we can print our list and for example there's also the function sorted

00:43.380 --> 00:54.290
that we can start our list and then ascending minor so here we have 2 3 5 6 and saw it actually creates

00:54.410 --> 00:56.390
a sorted copy off.

00:56.480 --> 01:03.740
So if we want to really sort our list out we have to reassign l.

01:04.230 --> 01:05.640
So now let's print again.

01:05.650 --> 01:15.570
Oh and here we have these sorted lists L and we already know methods which are functions that are attached

01:15.570 --> 01:23.190
to a specific objects for instance we know that the list object has and the method append where we can

01:23.190 --> 01:25.220
append another element to the end of the list.

01:25.220 --> 01:33.270
So for example we can here append the element 7 to the end of the list and let's print out our list

01:33.270 --> 01:37.670
again and there we have 4 7 at the end of the list.

01:37.960 --> 01:44.880
And as a general rule functions work for different objects so you can print out a list or an integer

01:44.880 --> 01:47.850
on float with the function print.

01:47.850 --> 01:53.320
And in contrast to that methods are functions specifically designed for particular objects.

01:53.320 --> 01:59.010
So for example here append the append method is a particular function for.

01:59.010 --> 02:02.000
For the list object.

02:02.270 --> 02:02.750
All right.

02:02.750 --> 02:08.360
And we already know more functions so we know the lengths function which calculates them the amount

02:08.360 --> 02:17.480
of elements in our list so we have five elements and we already know the main function which calculates

02:17.480 --> 02:20.150
that the minimum element of our list.

02:21.710 --> 02:24.800
So it s 2 and we also know the max function

02:29.400 --> 02:35.470
and we've already seen also the SUM function which takes them the sum over all elements in our list.

02:38.480 --> 02:44.200
Now we have seen that the output and functions like minimal maximum sum and one could think that now

02:44.270 --> 02:49.200
there should be also a mean function which calculates them the mean of our list.

02:49.200 --> 02:57.650
And if we try to apply that mean function to our list L and then Python to assess them that then I mean

02:57.650 --> 02:58.400
is not defined.

02:58.400 --> 03:06.100
So there is no need function and now we can create our own user defined mean function that calculates

03:06.100 --> 03:07.380
them the mean of.

03:07.440 --> 03:07.680
Mm.

03:07.680 --> 03:08.150
Yeah.

03:08.160 --> 03:14.140
List and before we start creating the function I mean we should first of all think about how to calculate

03:14.140 --> 03:16.600
the mean of a sequence of numbers and the.

03:16.630 --> 03:23.940
Actually we just have to take them the sum of them all elements divided by the amount of elements itself.

03:24.580 --> 03:27.290
So let's check what we get here.

03:27.520 --> 03:33.750
So we have here the sum is 23 and the length is five.

03:33.750 --> 03:39.040
So twenty three divided by five gives us four point six.

03:39.130 --> 03:46.000
And now what we can do we can yeah define our new function mean and yeah we have to start with the keyword

03:46.040 --> 03:47.180
death.

03:47.730 --> 03:51.430
Here you see it in green it's a keyword and then we say mean.

03:51.720 --> 03:59.820
So our function so to have the name mean and then within the brackets we define a parameter x so x could

03:59.910 --> 04:08.300
stand for a list or another type of sequence like a tuple or so on and then we have to make a call on

04:08.310 --> 04:14.100
and then if we press and uh we see that the python automatically creates here and then.

04:14.460 --> 04:18.470
So this is called the function header that we defined the function.

04:18.990 --> 04:26.160
And now here is the function body where we define what are the functions of actually calculate.

04:26.160 --> 04:35.220
And here we have to make an indent so the function should calculate the sum over our parameter x divided

04:35.220 --> 04:45.960
by the length of our parameter X then we can delete here the empty line and then we have to define what

04:45.960 --> 04:48.310
our function so to actually return.

04:48.310 --> 04:54.030
So here in the function body we define what the functions calculate and then in the end we should define

04:54.420 --> 04:56.430
what our function should return.

04:56.490 --> 05:02.080
So we use them the keyword return and the function of such return us them they mean.

05:02.100 --> 05:03.510
So now we can run the cell

05:06.230 --> 05:07.490
and here once again.

05:07.490 --> 05:14.690
So this is some of the same quote here if you compare it and then what we can do we can call the function

05:14.720 --> 05:17.930
bypassing the argument added to the function.

05:17.930 --> 05:23.450
So Ellis our list and the function expects an argument for the parameter x.

05:23.490 --> 05:26.190
So our argument is then the list L..

05:26.960 --> 05:29.710
And let's see what we get here.

05:29.720 --> 05:38.000
So here we get four point six and we can also do this more explicitly so we can call the function by

05:38.000 --> 05:43.150
saying okay our parameter X should be our list ello.

05:43.490 --> 05:48.250
So this is actually the same as above Yeah.

05:48.310 --> 05:54.020
And our function should not only work for lists but have also for other sequences like tuple so we can

05:54.020 --> 06:02.920
define that tuple t with the elements for five six seven and then we should also be able to calculate

06:02.920 --> 06:07.640
them the mean of our total because if you look at the function body here.

06:07.640 --> 06:14.080
So we calculate the sum of a sequence divided by the length of a sequence and some in length.

06:14.120 --> 06:20.930
Functions should also work for tuples so our function our user defined function means would also work

06:20.930 --> 06:21.610
for us.

06:21.620 --> 06:29.590
Let's see if it works and here we are and what I also want to show you.

06:29.590 --> 06:36.020
So there's no need to define yeah a variety of mean we can directly say okay.

06:36.090 --> 06:44.130
Please return me the sum of our sequence x divided by the length of our sequence X so this would also

06:44.130 --> 06:44.420
work

06:48.510 --> 06:54.650
and let's run it again and there can see it so it also works.

06:54.810 --> 07:00.540
All right so let's move to another example and we assign the integer seven to our arrival a

07:03.470 --> 07:05.950
and then we can calculate a squared.

07:07.520 --> 07:11.140
So it s forty nine yeah.

07:11.180 --> 07:12.680
Then if you want to change a.

07:12.710 --> 07:21.050
So let's say we want to change a two A's then we can rerun the cells and get sixty far and whenever

07:21.050 --> 07:27.860
we want to reuse a code we can also define a function so we can define the function square with them

07:27.890 --> 07:34.130
the parameter X and the function so to return us them the square of our parameter X

07:37.990 --> 07:43.120
so let's try for and call the function by passing the argument for

07:46.070 --> 07:49.900
and the function gives the sixteen and the same as above.

07:49.900 --> 07:57.060
We can also make it more explicitly by calling the function saying that the parameter X should be for

07:59.560 --> 08:06.100
so this is the same but actually what we have to do we have to pass the an argument to our function.

08:06.100 --> 08:11.500
So if we just call our function with no argument we get an error message.

08:12.670 --> 08:19.460
So you can see is our function squares missing 1 require positional argument x.

08:19.810 --> 08:26.820
However what we can do we can set a default value to our paramedics so we can define our functions create

08:26.830 --> 08:38.510
two by saying that the default value of our parameter X should be to and the function so return x cared.

08:38.520 --> 08:45.680
So now if you call the function without any argument the function automatically applies.

08:45.690 --> 08:47.320
Our default value too.

08:47.550 --> 08:56.580
So now we would expect here therefore as a return of the function and correct of course the function

08:56.580 --> 08:58.500
can also be called the new argument.

08:58.500 --> 09:08.010
So for example for gives us 16 and same as above we can make this more explicitly by saying our parameter

09:08.040 --> 09:09.080
X is for.

09:11.250 --> 09:13.380
And this also works.

09:13.380 --> 09:13.810
All right.

09:13.830 --> 09:18.510
These were the basics of youth that defined functions and the next two sat sessions we are going a bit

09:18.510 --> 09:20.850
more into detail so hope to see you there by.
